package me.w70.bot.wrappers;

import java.util.ArrayList;

import me.w70.bot.Bot;

public class RSInterfaces {

	public static RSInterfaceGroup getInterfaceGroup(int ID) {
		me.w70.hooks.Interface[][] interfaces = Bot.methods.getClient()
				.getInterfaces();
		if (interfaces == null)
			return null;
		if (interfaces[ID] == null)
			return null;
		return new RSInterfaceGroup(interfaces[ID], ID);
	}

	public synchronized static RSInterface[] getAllInterfaces() {

		ArrayList<RSInterface> ifaces = new ArrayList<RSInterface>();
		try {
			me.w70.hooks.Interface[][] interfaces = Bot.methods.getClient()
					.getInterfaces();
			if (interfaces == null)
				return null;
			for (int parent = 0; parent < interfaces.length; parent++) {
				if (interfaces[parent] == null)
					continue;
				for (int child = 0; child < interfaces[parent].length; child++) {
					me.w70.hooks.Interface iface = interfaces[parent][child];
					if (iface == null)
						continue;
					RSInterface anInterface = new RSInterface(
							interfaces[parent][child], null);
					anInterface.setIndex(child);
					ifaces.add(anInterface);
				}

			}
		} catch (NullPointerException ignored) {
			return null;
		}
		return ifaces.toArray(new RSInterface[ifaces.size()]);
	}

	public static RSInterface getInterface(int parentID, int childID) {
		try {
			me.w70.hooks.Interface[][] interfaces = Bot.methods.getClient()
					.getInterfaces();
			if (interfaces == null)
				return null;
			if (interfaces[parentID] == null)
				return null;
			if (interfaces[parentID][childID] == null)
				return null;
			RSInterface anInterface = new RSInterface(
					interfaces[parentID][childID], null);
			anInterface.setIndex(childID);
			return anInterface;
		} catch (NullPointerException ignored) {
			return null;
		}
	}

	public static RSInterface getInterface(int id) {
		return RSInterfaces.getInterface(id, 0);
	}

	/**
	 * Checks if an interface exists
	 * 
	 * @param groupID
	 * @param interfaceID
	 * @return
	 */
	public boolean interfaceExists(int groupID, int interfaceID) {
		return getInterface(groupID, interfaceID) != null;
	}

	/**
	 * Checks if an interface exists.
	 * 
	 * @param groupID
	 * @return
	 */
	public static boolean interfaceGroupExists(int groupID) {
		return getInterfaceGroup(groupID) != null;
	}

	public static int EQUIPMENT_HELMET = 0;

	public static int EQUIPMENT_CAPE = 1;

	public static int EQUIPMENT_AMULET = 2;

	public static int EQUIPMENT_WEAPON = 3;

	public static int EQUIPMENT_CHEST = 4;

	public static int EQUIPMENT_SHIELD = 5;

	public static int EQUIPMENT_STORAGE = 6;

	public static int EQUIPMENT_LEGS = 7;

	public static int EQUIPMENT_GLOVE = 9;

	public static int EQUIPMENT_BOOT = 10;

	public static int EQUIPMENT_RING = 12;

	/**
	 * Gets the item at a given position. positions is given in the constants
	 * begining with EQUIPMENT_.
	 * 
	 * @param positon
	 *            f.ex Interfaces.EQUIPMENT_HELMET
	 * @return an item. This might not be a valid.
	 */
	public RSItem getEquipmentItem(int positon) {
		RSInterface equipment = getInterface(387, 29);
		return new RSItem(equipment.getItemIDArray()[positon] - 1, equipment
				.getItemStackSizeArray()[positon]);
	}

	/**
	 * A method that searches through the interfaces for specified text. If it
	 * finds the text, the interface gets added to the array. Useful for things
	 * like "Click here to continue".
	 * 
	 * @author alowaniak
	 * @param text
	 *            The String the child interface should contain.
	 * @return An array of interfaces.
	 */
	public static RSInterface[] getInterfaces(String text) {
		text = text.toLowerCase();
		ArrayList<RSInterface> ifaces = new ArrayList<RSInterface>();
		try {
			me.w70.hooks.Interface[][] interfaces = Bot.methods.getClient()
					.getInterfaces();
			if (interfaces == null)
				return null;
			for (int parent = 0; parent < interfaces.length; parent++) {
				if (interfaces[parent] == null)
					continue;
				for (int child = 0; child < interfaces[parent].length; child++) {
					me.w70.hooks.Interface iface = interfaces[parent][child];
					if (iface == null)
						continue;
					if (iface.getText().toLowerCase().contains(text)) {
						RSInterface anInterface = new RSInterface(
								interfaces[parent][child], null);
						anInterface.setIndex(child);
						ifaces.add(anInterface);
					}
				}
			}
		} catch (NullPointerException ignored) {
			return null;
		}
		return ifaces.toArray(new RSInterface[ifaces.size()]);
	}

	/**
	 * A method that determines if you can click continue or not.
	 * 
	 * @author alowaniak
	 * @return true when you can click continue, false otherwise
	 */
	public boolean canContinue() {
		RSInterface[] psblConts = getInterfaces("Click here to continue");
		if (psblConts == null)
			return false;
		for (RSInterface iface : psblConts)
			if (iface.textContainsIgnoreCase("Click here to continue"))
				return true;
		return false;
	}

	/**
	 * A method that clicks the continue child interface.
	 * 
	 * @author alowaniak
	 * @return true when it clicked continue, false otherwise
	 */
	public boolean clickContinue() {
		RSInterface[] psblConts = getInterfaces("Click here to continue");
		if (psblConts == null)
			return false;
		RSInterface contIface = null;
		for (RSInterface iface : psblConts)
			if (iface.textContainsIgnoreCase("Click here to continue")) {
				contIface = iface;
				break;
			}
		if (contIface == null)
			return false;
		contIface.doClick();
		return true;
	}

	public RSInterface getInterface(String txt) {
		RSInterface[] ifaces = getInterfaces(txt);
		if (ifaces != null && ifaces.length > 0)
			return ifaces[0];
		return null;
	}

}
